import React from "react";
import testcss from "../css/test.scss"
import boot from "../../node_modules/bootstrap/dist/css/bootstrap.min.css"
import { mapStateToProps, mapDispatchToProps } from "../redux/action.js"
import { Provider, connect } from "react-redux"
import store from "../redux/reducer.jsx"
class Cart extends React.Component {
	constructor() {
		super()
		this.state = {
			list: [],
			arr:[],
			number:"结算(0)"
			
		}
	}
	componentWillMount() {
		store.subscribe(() => {
			this.setState({
				list: this.props.cart
			})
		})
		console.log("aa");
		console.log(this.state.list)
	}
	//结算
	//	allprice(){
	//		var allprice=0;
	//		for(var i in this.props.cart){
	//			allprice+=this.props.cart[i].num*this.props.cart[i].price;
	//		}
	//		return allprice
	//	}
	
	//统计选中的数量
	countNum(){
		
		var number=0;
		var list=this.state.arr;
		for(var i in list){
			for(var j in this.props.cart){
				if(list[i]==this.props.cart[j].id){
					number+=this.props.cart[j].num
				}
			}
		}
//		number="结算("+number+")"
//		this.setState({
//			number:number
//		})
return number
	}
	count(k) {
		//获取所有的文本框 如果val值在数组中 选中否则 不选中
		var list = this.state.arr;
		var s = list.findIndex((value, index, list) => value == k);
		if(s == -1) {
			list.push(k)
		} else {
			list.splice(s, 1)
		}
		this.setState({
			arr: list
		})
		this.countNum()
	}
	//计算选中的总价格
	total(){
		var list=this.state.arr;
		console.log(list)
		//在list中存储的是选中商品的id
		//需要到store.getstate().list中根据id计算总的价格
		var total=0;
		for(var i in list){
			for(var j in this.props.cart){
				if(list[i]==this.props.cart[j].id){
					total+=this.props.cart[j].num*this.props.cart[j].price;
				}
			}
		}
		return total
	}
	render() {
		return(
			<div className="cart">
				<div className="content">
					<table className="table table-striped">
							  <thead>
							    <tr>
							      <td>选择修炼</td>
							      <td>功法层级</td>
							      <td>功法名称</td>
							      <td>功法价格</td>
							      <td>功法数量</td>
							      <td>操作</td>
							    </tr>
							  </thead>
							  <tbody>
								{this.props.cart.map((item,index)=>{
									return <tr key={index}>
													<td>
														<input type="checkbox" onClick={()=>{this.count(item.id)}} value={item.id}/>
													</td>
													<td><img src={item.src}/></td>
													<td>{item.goodsname}</td>
													<td>{item.price}</td>
													<td>
														<button className="btn btn-xs" onClick={()=>{this.props.cubNum1(index)}}>-</button>
														{item.num}
														<button className="btn btn-xs" onClick={()=>{this.props.addNum1(index)}}>+</button>
													</td>
													<td>
														<button className="btn btn-primary" onClick={()=>{this.props.del(index)}}>删除</button>
													</td>
											</tr>
								})}
							  </tbody>
						</table>
					</div>
					<div className="footer">
						<div>
							<div className="total">金额:{this.total()}</div>
							<div>
							<button className="btn btn-primary">结算{this.countNum()}</button>
							</div>
						</div>
					</div>
			</div>
		)
	}
}
Cart = connect(mapStateToProps, mapDispatchToProps)(Cart)
export default Cart
